/*
 * LengthFramer.c
 *
 *  Created on: 2012. 5. 8.
 *      Author: yjpark
 */
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "Protocol.h"

// complete
int Length_PutMsg(CalcInfo *ci, uint8_t *outBuf, size_t bufSize, size_t hSize, FILE *channel) {

	// message size
	int mSize = 0;

	// move for header
	memmove(outBuf+hSize, outBuf, bufSize-hSize);

	// header into outBuf
	// binary encode
	memcpy(outBuf, ci, hSize);

	// header+data into channel
	if((mSize = fwrite(outBuf, sizeof(uint8_t), bufSize, channel)) < 0)
		return -1;

	// send message
	fflush(channel);

	return mSize;
}

// complete
int Length_GetNextMsg(uint8_t *inBuf, size_t bufSize, FILE *fp) {
	long mSize = fread(inBuf, sizeof(uint8_t), bufSize, fp);

	if(mSize < 0)
		return -1;

	return (size_t) mSize;
}
